﻿/*----------------------------------------------------------------
//Copyright (C) 2012 赵国伟
//文件功能描述：订单管理客户端脚本
//创建人：赵国伟
//生成时间：2012-06-06 22:07:50
//----------------------------------------------------------------*/
var AjaxServerUrl = "/Manage/AS/OrdersCommon.aspx";
var ModelOfOrder;
var ModelOfLine;
var SelectOptionOfPayMode;
var SelectOptionOfSendMode;
var SelectOptionOfStatus;
var SelectOptionOfProvince = "";
var OrderID = 0;
var TableOfLines = "TableOfLines";
var CurRowOfLines = 0;
var CtrlOfSelGoods;
var CurDate;
var StatusEnum = { "等待发货": "等待发货", "等待付款": "等待付款","已完成":"已完成","已取消":"已取消" };
///初始化
function Init() {
    OrderID = 0;
    CurRowOfLines = 0;
}

//执行搜索；
function GoToSearch() {
    var url = "OrdersManage.aspx?";
    url += "OrderID=" + escape($("#txtKeyWordsOfSearch").val());
    url += "&UserName=" + escape($("#txtUserNameOfSearch").val());
    url += "&PayMode=" + escape($("#ddlPayModeOfSearch").val());
    url += "&SendMode=" + escape($("#ddlSendModeOfSearch").val());
    url += "&StartDate=" + $("#txtStartDateOfSearch").val();
    url += "&EndDate=" + $("#txtEndDateOfSearch").val();
    url += "&Status=" + escape($("#ddlStatusOfSearch").val());
    location.href = url;
}


//执行删除
function Delete(id) {
    if (!confirm("系统提示：您确定要删除该条订单信息？"))
        return;
    AjaxGet(AjaxServerUrl + "?Action=DeleteOrder&OrderID=" + id);
}

///检查表单；
function CheckForm() {
    if ($("#txtOrderDate").val() == "") {
        alert("系统提示：请选择订购日期！");
        $("#txtOrderDate").focus();
        return false;
    }
    if ($("#txtReceiverName").val() == "") {
        alert("系统提示：请输入收货人名称！");
        $("#txtReceiverName").focus();
        return false;
    }
    if ($("#txtReceiverZipCode").val() == "") {
        alert("系统提示：请输入邮编！");
        $("#txtReceiverZipCode").focus();
        return false;
    }
    if (!IsNumber($("#txtReceiverZipCode").val()) || $("#txtReceiverZipCode").val().length !=6) {
        alert("系统提示：邮编必须为数字,且长度必须为6位！");
        $("#txtReceiverZipCode").focus();
        return false;
    }

    if ($("#ddlProvince").val() == "") {
        alert("系统提示：请选择省份！");
        $("#ddlProvince").focus();
        return false;
    }
    if ($("#ddlCity").val() == "") {
        alert("系统提示：请选择城市！");
        $("#ddlCity").focus();
        return false;
    }
    if ($("#ddlArea").val() == "") {
        alert("系统提示：请选择区域！");
        $("#ddlArea").focus();
        return false;
    }
    if ($("#txtReceiverAddress").val() == "") {
        alert("系统提示：请输入收货人详细地址！");
        $("#txtReceiverAddress").focus();
        return false;
    }
    if ($("#txtReceiverMobile").val() == "" && $("#txtReceiverPhone").val()=="") {
        alert("系统提示：请输入收货人手机或固定电话！");
        return false;
    }
    if ($("#txtSendFee").val() == "" || !isMoney($("#txtSendFee").val())) {
        alert("系统提示：运费输入错误！");
        $("#txtSendFee").focus();
        return false;
    }
    if (parseFloat($("#txtSendFee" + i).val()) < 0) {
        alert("系统提示：运费不能小于0！");
        $("#txtSendFee").focus();
        return false;
    }

    //对订购明细的判断；
    if (CurRowOfLines == 0) {
        alert("系统提示：您还没有添加购买的商品！");
        return false;
    }
    for (var i = 0; i < CurRowOfLines; ++i) {
        if ($("#txtQuantityOf" + i) != null) {
            var goodsName = $("#hfGoodsNameOf" + i).val();

            if ($("#txtPriceOf" + i).val() == "" || !isMoney($("#txtPriceOf" + i).val())) {
                alert("系统提示：" + goodsName + " 的单价输入错误！");
                $("#txtPriceOf" + i).focus();
                return false;
            }
            if (parseFloat($("#txtPriceOf" + i).val()) < 0) {
                alert("系统提示：" + goodsName + "的单价不能小于0！");
                $("#txtPriceOf" + i).focus();
                return false;
            }

            if ($("#txtQuantityOf" + i).val() == "" || !IsNumber($("#txtQuantityOf" + i).val())) {
                alert("系统提示：" + goodsName + " 的数量必须为数字！");
                $("#txtQuantityOf" + i).focus();
                return false;
            }
            if (parseInt($("#txtQuantityOf" + i).val(), 10) < 0) {
                alert("系统提示：" + goodsName + "的数量不能小于0！");
                $("#txtQuantityOf" + i).focus();
                return false;
            }

            if ($("#txtPresentScoresOf" + i).val() == "" || !IsNumber($("#txtQuantityOf" + i).val())) {
                alert("系统提示：" + goodsName + " 的赠送积分必须为数字！");
                $("#txtPresentScoresOf" + i).focus();
                return false;
            }
            if (parseInt($("#txtPresentScoresOf" + i).val(), 10) < 0) {
                alert("系统提示：" + goodsName + "的赠送积分不能小于0！");
                $("#txtPresentScoresOf" + i).focus();
                return false;
            }
            
        }
    }

    return true;
}

//保存；
function Save() {
    $("#btnSave").attr("disabled", true);
    if (!CheckForm()) {
        $("#btnSave").attr("disabled", false);
        return;
    }
    var data = clone(ModelOfOrder);
    data = { "Lines": [], "Order": { "_areaid": 0, "_couponmoney": 0, "_couponsn": null, "_expresssn": null, "_fapiao": null, "_orderdate": "\/Date(1339409086019+0800)\/", "_orderid": 0, "_orderstatus": null, "_paymode": null, "_receiveraddress": null, "_receivermobile": null, "_receivername": null, "_receiverphone": null, "_receiverzipcode": null, "_remark": null, "_sendfee": 0, "_sendmode": null, "_totalamount": 0, "_userid": 0 }, "UserName": null };
    data.Order._orderid = OrderID;
    data.Order._orderdate = "/Date(" + DateStringToUTC($("#txtOrderDate").val()) + ")/"; 
    data.Order._receivername = $("#txtReceiverName").val();
    data.Order._receiverzipcode = $("#txtReceiverZipCode").val();
    data.Order._areaid = $("#ddlArea").val();
    data.Order._receiveraddress = $("#txtReceiverAddress").val();
    data.Order._receivermobile = $("#txtReceiverMobile").val();
    data.Order._receiverphone = $("#txtReceiverPhone").val();
    data.Order._paymode = $("#ddlPayMode").val();
    data.Order._sendmode = $("#ddlSendMode").val();
    data.Order._sendfee = $("#txtSendFee").val();

    data.Order._fapiao = $("#txtFaPiao").val();
    data.Order._remark = $("#txtRemark").val();
    data.Order._orderstatus = $("#ddlOrderStatus").val();

    //订购清单信息的处理；
    for (var i = 0; i < CurRowOfLines; ++i) {
        if ($("#txtQuantityOf" + i) != null) {
            var l = clone(ModelOfLine);
            l = { "GoodsName": null, "LineInfo": { "_discount": 0, "_goodsid": 0, "_orderid": 0, "_orderlineid": 0, "_presentscores": 0, "_price": 0, "_quantity": 0} };
            l.LineInfo._goodsid = $("#hfGoodsIDOf" + i).val();
            l.LineInfo._packunitid = $("#hfPackUnitIDOf" + i).val();
            l.LineInfo._orderid = OrderID;
            l.LineInfo._price = $("#txtPriceOf" + i).val();
            l.LineInfo._quantity = $("#txtQuantityOf" + i).val();
            l.LineInfo._presentscores = $("#txtPresentScoresOf" + i).val();
            l.LineInfo._discount = 1;
            l.GoodsName = $("#hfGoodsNameOf" + i).val();
            data.Lines.push(l);
        }
    }
    var url = AjaxServerUrl + "?Action=SaveOrder";
    AjaxPost(url, "btnSave", data);  //更新
}

//绑定订单信息到录入表单中；
function BindOrderToForm() {
    var url = AjaxServerUrl + "?action=GetOrderInfo&OrderID=" + OrderID + "&tttt=" + Math.random();
    $.ajax(
    {
        type: "Get",
        url: url,
        dataType: "json",
        success: function (data) {
            if (data != null) {
                $("#spUserName").html(data.UserName);
                $("#spOrderID").html(data.Order._orderid);
                $("#txtOrderDate").val(ConvertJsonDatetoToLocateDate(data.Order._orderdate));
                $("#txtReceiverName").val(data.Order._receivername);
                $("#txtReceiverZipCode").val(data.Order._receiverzipcode);
                $("#ddlProvince").val(data.Area.Province._id);
                QueryCities(data.Area.Province._id, 'ddlCity', data.Area.Area._cityid);
                QueryAreas(data.Area.Area._cityid, 'ddlArea', data.Order._areaid);

                if (data.Order._couponsn != null) {
                    $("#trCouponSN").show();
                    $("#spCouponSN").html(data.Order._couponsn);
                    $("#spCouponMoney").html(data.Order._couponmoney);
                }

                $("#txtReceiverAddress").val(data.Order._receiveraddress);
                $("#txtReceiverMobile").val(data.Order._receivermobile);
                $("#txtReceiverPhone").val(data.Order._receiverphone);
                $("#ddlPayMode").val(data.Order._paymode);
                $("#ddlSendMode").val(data.Order._sendmode);
                $("#txtSendFee").val(data.Order._sendfee);
                $("#txtFaPiao").val(data.Order._fapiao);
                $("#txtRemark").val(data.Order._remark);
                $("#ddlOrderStatus").val(data.Order._orderstatus);

                //对订购清单的输出。
                for (var i = 0; i < data.Lines.length; ++i) {
                    var l = data.Lines[i];
                    AddLine(l.LineInfo._goodsid, l.GoodsName, l.LineInfo._price, l.PackName , l.LineInfo._packunitid);
                    $("#txtPriceOf" + i).val(l.LineInfo._price);
                    $("#txtQuantityOf" + i).val(l.LineInfo._quantity);
                    $("#txtPresentScoresOf" + i).val(l.LineInfo._presentscores);
                }

            }
        }
    });
}




///添加界面；
function Add() {
    Init();
    GetPopupDiv(700, 800, "#e2e2e2", "添加订单", GetHtmlForm());
    CtrlOfSelGoods = new SelectGoods('btnSelGoods', 'Single', null, null, AddLine);
    $("#spUserName").html("dianke123");
    $("#spOrderID").html("新订单");
}

//编辑界面；
function Edit(id) {
    Init();
    OrderID = id;
    GetPopupDiv(700, 800, "#e2e2e2", "编辑订单", GetHtmlForm());
    CtrlOfSelGoods = new SelectGoods('btnSelGoods', 'Single', null, null, AddLine);
    BindOrderToForm();
}

///获取录入界面的HTML;
function GetHtmlForm() {
    var html = new Array();
    html.push("<form>");
    html.push("<table width=\"800\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"table\">");
    html.push("  <tr>");
    html.push("    <td colspan=\"4\" class=\"HeadTitle\">&gt;&gt;订单基本信息</td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td width=\"90\" align=\"right\">订单号：</td>");
    html.push("    <td colspan=\"3\"><span id=\"spOrderID\"></span></td>");
    html.push("    </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">订购日期：</td>");
    html.push("    <td width=\"230\"><input name=\"txtOrderDate\" type=\"text\" id=\"txtOrderDate\" size=\"12\" class=\"Wdate\" readonly=\"readonly\" value=\"" + CurDate + "\" onfocus=\"new WdatePicker({el:this})\" style=\"cursor: hand\" />");
    html.push("      <span class=\"red\">(*)</span></td>");
    html.push("    <td width=\"108\">所属会员：</td>");
    html.push("    <td width=\"259\"><span id=\"spUserName\"></span>");
    html.push("    </td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">收货人：</td>");
    html.push("    <td><input name=\"txtReceiverName\" type=\"text\" id=\"txtReceiverName\" size=\"20\" class=\"border6\" />");
    html.push("      <span class=\"red\">(*)</span></td>");
    html.push("    <td>邮编：</td>");
    html.push("    <td><input name=\"txtReceiverZipCode\" type=\"text\" id=\"txtReceiverZipCode\" size=\"6\"  maxlength=\"6\"  class=\"border6\"/>");
    html.push("      <span class=\"red\">(*)</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">收货人地址：</td>");
    html.push("    <td colspan=\"3\"><select name=\"ddlProvince\" id=\"ddlProvince\"  onchange=\"QueryCities(this.options[this.selectedIndex].value,'ddlCity');\">");
    html.push("      <option value=\"\">-省份-</option>");
    html.push(SelectOptionOfProvince);
    html.push("    </select>");
    html.push("      <select name=\"ddlCity\" id=\"ddlCity\"  onchange=\"QueryAreas(this.options[this.selectedIndex].value,'ddlArea');\">");
    html.push("        <option value=\"\">-城市-</option>");
    html.push("            </select>");
    html.push("      <select name=\"ddlArea\" id=\"ddlArea\">");
    html.push("        <option value=\"\">-区域-</option>");
    html.push("            </select>");
    html.push("      <input name=\"txtReceiverAddress\" type=\"text\" id=\"txtReceiverAddress\" size=\"50\" class=\"border6\"/>");
    html.push("      <span class=\"red\">(*)</span></td>");
    html.push("    </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">手机号：</td>");
    html.push("    <td><input name=\"txtReceiverMobile\" type=\"text\" id=\"txtReceiverMobile\" size=\"15\" class=\"border6\"/></td>");
    html.push("    <td>固定电话</td>");
    html.push("    <td><input name=\"txtReceiverPhone\" type=\"text\" id=\"txtReceiverPhone\" size=\"20\" class=\"border6\"/></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">支付方式：</td>");
    html.push("    <td colspan=\"3\"><select name=\"ddlPayMode\" id=\"ddlPayMode\">");
    html.push(SelectOptionOfPayMode);
    html.push("    </select>    </td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">送货方式：</td>");
    html.push("    <td colspan=\"3\"><select name=\"ddlSendMode\" id=\"ddlSendMode\">");
    html.push(SelectOptionOfSendMode);
    html.push("            </select>");
    html.push("      送货费用：￥");
    html.push("      <input name=\"txtSendFee\" type=\"text\" id=\"txtSendFee\" value=\"0\" size=\"10\" class=\"border6\"/></td>");
    html.push("  </tr>");
    html.push("  <tr id=\"trCouponSN\" style=\"display:none;\">");
    html.push("    <td align=\"right\">优惠劵：</td>");
    html.push("    <td colspan=\"3\">卡号：<span id=\"spCouponSN\"></span> 金额：￥<span id=\"spCouponMoney\">0.00</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">发票抬头：</td>");
    html.push("    <td colspan=\"3\"><input name=\"txtFaPiao\" type=\"text\" id=\"txtFaPiao\" size=\"60\" class=\"border6\" /></td>");
    html.push("    </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">订单备注：</td>");
    html.push("    <td colspan=\"3\"><textarea name=\"txtRemark\" cols=\"70\" rows=\"3\" id=\"txtRemark\"></textarea></td>");
    html.push("    </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">订单状态：</td>");
    html.push("    <td colspan=\"3\"><select name=\"ddlOrderStatus\" id=\"ddlOrderStatus\">");
    html.push(SelectOptionOfStatus);
    html.push("      </select>");

    html.push("<span id=\"spExpressSn\" style=\"display:none;\"> 快递单号：<input name=\"txtExpressSN\" type=\"text\" id=\"txtExpressSN\" value=\"\" size=\"10\" class=\"border6\"/></span>");

    html.push("      </td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td colspan=\"4\" align=\"center\"><input name=\"btnSave\" type=\"button\" id=\"btnSave\" value=\" 保 存 \" class=\"btn2\" onclick=\"Save();\">");
   // html.push("      <input name=\"btnReset\" type=\"reset\" id=\"btnReset\" value=\" 重 置 \" class=\"btn2\">");
    html.push("      <input name=\"btnClose\" type=\"button\" id=\"btnClose\" value=\"关闭本窗口\" class=\"btn2\" onclick=\"yesOther(false);\"></td>");
    html.push("    </tr>");
    html.push("</table>");
    html.push("<table width=\"800\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"table\" id=\"TableOfLines\">");
    html.push("  <tr>");
    html.push("    <td colspan=\"8\" class=\"HeadTitle\">&gt;&gt;订购清单 <input name=\"btnSelGoods\" type=\"button\" id=\"btnSelGoods\" value=\"选择商品..\" class=\"btn2\"  />  </td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td width=\"44\" align=\"center\">编号</td>");
    html.push("    <td align=\"center\">商品名称</td>");
    html.push("    <td width=\"60\" align=\"center\">包装</td>");
    html.push("    <td width=\"90\" align=\"center\">单价</td>");
    html.push("    <td width=\"90\" align=\"center\">订购数量</td>");
    html.push("    <td width=\"90\" align=\"center\">积分赠送</td>");
    html.push("    <td width=\"90\" align=\"center\">金额</td>");
    html.push("    <td width=\"90\" align=\"center\">操作</td>");
    html.push("  </tr>");

    html.push("<tr id=\"trNoLine\">");
    html.push("<td colspan=\"8\" class=\"red\" align=\"center\">[ 还没有加入任何商品！ ]</td>");
    html.push("</tr>");

    html.push("  <tr>");
    html.push("    <td colspan=\"4\" align=\"right\">&nbsp;</td>");
    html.push("    <td align=\"center\"><span id=\"spTotalQuantity\">0</span></td>");
    html.push("    <td align=\"right\">&nbsp;</td>");
    html.push("    <td align=\"center\">￥<span id=\"spTotalMoney\">0</span></td>");
    html.push("    <td align=\"center\">&nbsp;</td>");
    html.push("  </tr>");
    html.push("</table>");
    html.push("</form>");

    return html.join("");

}


function UpdateLineCounts(i)
{
    var t = parseInt($("#txtQuantityOf" + i).val(), 10);
    $("#spLineCountsOf" + i).html(parseFloat($("#txtPriceOf" + i).val()).Mul(t));
    UpdateCounts();
   
}

///更新统计信息；
function  UpdateCounts() {
    var totalQuantity = 0;
    var totalMoney = 0.00;
    for (var i = 0; i < CurRowOfLines; ++i) {
        if ($("#txtQuantityOf" + i) != null && $("#txtQuantityOf" + i) != undefined) {
            var t = parseInt($("#txtQuantityOf" + i).val(), 10);
            totalQuantity += t;
            var s = parseFloat($("#txtPriceOf" + i).val());
            totalMoney += s.Mul(t);
        }
    }
    $("#spTotalQuantity").html(totalQuantity);
    $("#spTotalMoney").html(totalMoney);

}



function AddLine(goodsId, goodsName, saleprice, packName,packUnitId) {

    $("#trNoLine").hide();
    var tr = oo(TableOfLines).insertRow(oo(TableOfLines).rows.length - 1);
    td = tr.insertCell(0);
    td.innerHTML = ((CurRowOfLines + 1) + ".");  //序号列；
    td.align = "center";

    td = tr.insertCell(1);
    td.innerHTML = "<a href=\"/Show.aspx?GoodsID=" + goodsId + "\" target=\"_blank\">" + goodsName + "</a><input type=\"hidden\" name=\"hfGoodsIDOf" + CurRowOfLines + "\" id=\"hfGoodsIDOf" + CurRowOfLines + "\" value=\"" + goodsId + "\" />" + "<input type=\"hidden\" name=\"hfGoodsNameOf" + CurRowOfLines + "\" id=\"hfGoodsNameOf" + CurRowOfLines + "\" value=\"" + goodsName + "\" />";
    td.align = "center";

    td = tr.insertCell(2);
    td.innerHTML = packName + "<input type=\"hidden\" name=\"hfPackUnitIDOf" + CurRowOfLines + "\" id=\"hfPackUnitIDOf" + CurRowOfLines + "\" value=\"" + packUnitId + "\" />";
    td.align = "center";

    td = tr.insertCell(3);
    td.innerHTML = "￥<input name=\"txtPriceOf" + CurRowOfLines + "\" type=\"text\" id=\"txtPriceOf" + CurRowOfLines + "\" size=\"8\" class=\"border6\" value=\"" + saleprice + "\" onblur=\"UpdateLineCounts(" + CurRowOfLines + ")\" />";
    td.align = "center";

    td = tr.insertCell(4);
    td.innerHTML = "<input name=\"txtQuantityOf" + CurRowOfLines + "\" type=\"text\" id=\"txtQuantityOf" + CurRowOfLines + "\" size=\"8\" class=\"border6\" value=\"1\" onblur=\"UpdateLineCounts(" + CurRowOfLines + ")\"  />";
    td.align = "center";

    td = tr.insertCell(5);
    td.innerHTML = "<input name=\"txtPresentScoresOf" + CurRowOfLines + "\" type=\"text\" id=\"txtPresentScoresOf" + CurRowOfLines + "\" size=\"8\" class=\"border6\" value=\"0\" />";
    td.align = "center";

    td = tr.insertCell(6);
    td.innerHTML = "￥<span id=\"spLineCountsOf" + CurRowOfLines + "\">" + saleprice + "</span>";
    td.align = "center";


    td = tr.insertCell(7);
    td.innerHTML = "[ <a href=\"javascript:void(0);\" onclick=\"javascript:DeleteLine(this);\" >删除</a> ]";

    
   
    td.align = "center";
    CurRowOfLines++;
    UpdateLineCounts(CurRowOfLines-1);
}

function DeleteLine(objRow) {
    if (!confirm("确定要删除该行吗?"))
        return false;
    var rowIndex = objRow.parentNode.parentNode.rowIndex;
    oo(TableOfLines).deleteRow(rowIndex);
    CurRowOfLines--;
    UpdateCounts();
    if (oo(TableOfLines).rows.length == 4) {
        CurRowOfLines = 0;
        $("#trNoLine").show();
    }

}











